﻿<?php
class Model_News extends Zend_Db_Table_Abstract
{
	protected $_name = 'news';

	public function createNews ($n_tieude, $n_noidung, $n_anhdd)
	{
		$rowNews= $this->createRow();
		if ($rowNews) {
			// update the row values
			$rowNews->n_tieude = $n_tieude;
			$rowNews->n_anhdd = $n_anhdd;
			$rowNews->n_noidung = $n_noidung;
			
			//$date = new Zend_Date();
			//$date = $date->toString('Y-M-d HH:m:s');
			//$rowNews->n_ngay = time();
			$rowNews->save();					
			return $rowNews;
		} else {
			throw new Zend_Exception("Không thể tạo tin này!!!");
		}
	}
	
	public function fetchPaginatorAdapter()
	{
		$select=$this->select();
		$adapter=new Zend_Paginator_Adapter_DbTableSelect($select);
		return $adapter;
	}
	
	public static function getNewsDetails($n_id)
	{
		$newsDetailModel = new self();
		$select = $newsDetailModel->select();
		$select->order(array('n_ngay DESC'))
			   ->where('n_id =?',$n_id);	
		return $newsDetailModel->fetchAll($select);
	}
	
	public static function getViewNews($n_id)  {
		$viewNewsModel = new self();
		$select = $viewNewsModel->select();
		$select->order(array('n_ngay DESC'))
			   ->where('n_id >?',$n_id)
			   ->orWhere('n_id<?',$n_id);
		return $viewNewsModel->fetchAll($select);
	}
	
	public function orderViewNews()  {
		$orderNewsModel = new self();
		$select = $orderNewsModel->select();
		$select->order(array('n_ngay DESC'));
		return $orderNewsModel->fetchAll($select);
	}
	
	public function getNewsAll()
	{
		$newsModel = new self();
		$select = $newsModel->select();
		$select->order(array('n_ngay'));	
		return $newsModel->fetchAll($select);
	
	}
	
	public static function getNewsLimit($start,$count)
	{
		$newsModel = new self();
		$select = $newsModel->select();
		$select->order(array('n_ngay'))
			   ->limit($start,$count);	
		return $newsModel->fetchAll($select);
	}	
	
	public function deleteNewsById($nid)
	{
		$newsModel = new self();
		$where = $newsModel->getAdapter()->quoteInto('n_id = ?', $nid);
		if($where){
			$newsModel->delete($where);
		}
		else{
			throw new Zend_Exception("Could not delete news");
		}
	}
	
	public function updateNews($n_id,$n_tieude,$n_noidung)
	{
		$where = $this->getAdapter()->quoteInto('n_id =?',$n_id);
		$set = array('n_tieude' =>$n_tieude,'n_noidung'=>$n_noidung);
		$rows_affected = $this->update($set, $where);
	}
}
